Notepad = Ext.extend(Ext.app.Module, {
    id: 'notepad-win',
	textArea: new Ext.form.TextArea,
    init: function(){
            this.launcher = {
                    text: 'Futura Notepad',
                    iconCls: 'icon-grid',
                    handler: this.createWindow,
                    scope: this
            }
    },

    createWindow: function() {
            var desktop = this.app.getDesktop();
			var self = this;
            var win = desktop.getWindow('notepad-win');
            if(!win){
                    win = desktop.createWindow({
                            id: 'notepad-win',
                            title: 'Futura Notepad',
                            iconCls: 'icon-grid',
                            width: 600,
                            height: 400,
                            layout:'fit',
                            maximizable: true,
                            minimizable: true,
                            items: this.textArea,
                            tbar: [
								{ text: 'New', handler: function() { this.textArea.setValue(""); }, scope: this },
								'-',
								{ text: 'Open', handler: this.openDialog, scope: this },
								{ text: 'Save', handler: this.saveDialog, scope: this }
							]
                    });
            }
			win.on('close', function() {
				self.textArea.setValue('');
			});
            win.show();
    },
    
    openDialog: function() {
		var self = this;
		var dialog = new OpenDialog(function(filename) {
			self.loadD(filename);
		});
		dialog.show();
    },

	saveDialog: function() {
		var self = this;
		var dialog = new SaveDialog(function(filename) {
			self.saveD(filename);
		});
		dialog.show();
	},

	loadD: function(file) {
		var self = this;
		Ext.Ajax.request({
			url: '../php/read_file.php',
			params: { filename: file },
			success: function(response) {
				self.textArea.setValue(response.responseText);
			}
		});
	},

	saveD: function(file) {
		var self = this;
		Ext.Ajax.request({
			url: '../php/save_file.php',
			params: { filename: file, content: self.textArea.getValue() },
			success: function(response) {
				Ext.Msg.alert("Status", response.responseText);
			}
		});
	}
});
